package club.yuit.desktop.ui.topics.cut;


import club.yuit.desktop.ui.UITools;
import club.yuit.rmq.service.RMQClusterService;
import com.formdev.flatlaf.FlatLightLaf;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.awt.*;


/**
 * @author: yuit
 * @date: 2023/12/12 15:44
 */
@Slf4j
public class CreateTopicDialog  extends JDialog {

    private JPanel mainPanel;

    private final CreateOrUpdateTopicFormPanel formPanel;

    private final RMQClusterService service;


    public CreateTopicDialog(Component relativeTo, RMQClusterService service) {
        this.service = service;
        this.mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        setTitle("新增/修改Topic");
        this.setSize(new Dimension(500,600));

        formPanel = new CreateOrUpdateTopicFormPanel(this.service);
        JScrollPane scrollPane = new JScrollPane(formPanel);
        scrollPane.getVerticalScrollBar().setUnitIncrement(15);
        scrollPane.setBorder(null);
        mainPanel.add(scrollPane);
        mainPanel.add(controlPanel());
        this.setContentPane(mainPanel);
        this.setLocationRelativeTo(relativeTo);
        ImageIcon icon = UITools.getSVGIcon("/imgs/Icon.svg",16,16);
        this.setIconImage(icon.getImage());
        this.setModal(true);
    }

    public static void main(String[] args) {
        FlatLightLaf.setup();
        new CreateTopicDialog(null,null).setVisible(true);
    }

    private JPanel controlPanel() {
        JPanel panel = new JPanel();

        panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        JButton ok=new JButton("确定");
        JButton cancel=new JButton("取消");
        ok.addActionListener(e -> {
            try {
                service.createOrUpdateTopic(formPanel.getTopicConfig());
                CreateTopicDialog.this.dispose();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }

        });

        cancel.addActionListener(e -> {
            CreateTopicDialog.this.dispose();
        });


        panel.add(ok);
        panel.add(cancel);

        panel.setMaximumSize(new Dimension(Integer.MAX_VALUE,panel.getPreferredSize().height));

        return panel;
    }
}

